package controlador;

import java.awt.Color;
import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.SQLException;

import modelo.Conexion;

import vista.FormReporte;

public class ControlReporte
{
	private FormReporte frm;
	private int  n = 0;
	private int c = 0;
	private boolean ascend = false;
	
	public ControlReporte(int tipo)
	{
		frm = new FormReporte();
		frm.setLocationRelativeTo(null);
		new Descendente();
		new Ascendente();
		switch(tipo)
		{
		   case 1:
			   {
				   MostrarTabla1();
				   frm.setTitle("Listado de Productos mas vendidos(Cantidad)");
				   break;
			   }
		   case 2:
			  {
				   MostrarTabla2();
				   frm.setTitle("Listado de Ingredientes mas utilizados");
				   break;
			  }
		  case 3:
			  {
				   MostrarTabla3();
				   frm.setTitle("Listado de Productos mas vendidos(Monto)");
				   break;
			  }
		}
		frm.setVisible(true);
	}
	public class Ascendente  implements ActionListener
	{
		public Ascendente()
		{
			frm.addlistenerAsc(this);
		}
		public void actionPerformed(ActionEvent e)
		{
			if(!ascend)
			{
				frm.InvertirValores(n,c);
			    ascend = true;
			}
			frm.CargarTabla();
		}
	};
	public class Descendente  implements ActionListener
	{
		public Descendente()
		{
			frm.addlistenerDes(this);
		}
		public void actionPerformed(ActionEvent e)
		{
			if(ascend) 
			{
				frm.InvertirValores(n,c);
				ascend = false;
			}
			frm.CargarTabla();
		}
	};
	void MostrarTabla1()
	{
		try
		{
			Conexion conexion= new Conexion();
			String SQL = "SELECT DISTINCT codproducto,SUM(cantidad) AS T FROM " +
					"ordenpedido GROUP BY codproducto ORDER BY T DESC";
			ResultSet registro = conexion.consultar(SQL);
			n = 0;
			while(registro.next())n++;
			registro = conexion.consultar(SQL);
			Object VAL[][] = new Object[n][5];
			String T[] = new String[]{"Codigo","Descripcion","Precio","Cantida","Ingreso"}; 
			n = 0;
			c = 5;
			while(registro.next())
			{
				ResultSet R = conexion.consultar(
                "SELECT * FROM producto WHERE codproducto = '" + registro.getString("codproducto") + "'");
				if(R.next())
				{
					VAL[n][0] = registro.getString("codproducto");
					VAL[n][1] = R.getString("descripcion");
					VAL[n][2] = R.getFloat("preciou");
					VAL[n][3] = registro.getInt("T");
					VAL[n++][4] = R.getFloat("preciou") * registro.getInt("T");
				}
			}
			frm.CargarTabla(VAL,T);
		}
		catch(SQLException e)
	    {
			e.printStackTrace();
	    }
	}
	void MostrarTabla2()
	{
		try
		{
			Conexion conexion= new Conexion();
			String SQL = "SELECT DISTINCT codingrediente,COUNT(cantidadingrediente) AS T FROM " +
					"productoingrediente GROUP BY codingrediente ORDER BY T DESC";
			ResultSet registro = conexion.consultar(SQL);
			n = 0;
			while(registro.next())n++;
			registro = conexion.consultar(SQL);
			Object VAL[][] = new Object[n][5];
			String T[] = new String[]{"Codigo","Nombre","Existencia","Total"}; 
			n = 0;
			c = 4;
			while(registro.next())
			{
				ResultSet R = conexion.consultar(
                "SELECT * FROM ingrediente WHERE codingrediente = '" + registro.getString("codingrediente") + "'");
				
				if(R.next())
				{
					VAL[n][0] = R.getString("codingrediente");
					VAL[n][1] = R.getString("nombre");
					VAL[n][2] = R.getFloat("existencia");
					VAL[n++][3] = registro.getInt("T");
				}
			}
			frm.CargarTabla(VAL,T);
		}
		catch(SQLException e)
	    {
			e.printStackTrace();
	    }
	}
	void MostrarTabla3()
	{
		try
		{
			Conexion conexion= new Conexion();
			String SQL = "SELECT DISTINCT codproducto,SUM(totalp) AS T FROM " +
					"ordenpedido GROUP BY codproducto ORDER BY T DESC";
			ResultSet registro = conexion.consultar(SQL);
			n = 0;
			while(registro.next())n++;
			registro = conexion.consultar(SQL);
			Object VAL[][] = new Object[n][5];
			String T[] = new String[]{"Codigo","Descripcion","Precio","Ingreso Total"}; 
			n = 0;
			c = 4;
			while(registro.next())
			{
				ResultSet R = conexion.consultar(
                "SELECT * FROM producto WHERE codproducto = '" + registro.getString("codproducto") + "'");
				if(R.next())
				{
					VAL[n][0] = registro.getString("codproducto");
					VAL[n][1] = R.getString("descripcion");
					VAL[n][2] = R.getFloat("preciou");
					VAL[n++][3] =  registro.getInt("T");
				}
			}
			frm.CargarTabla(VAL,T);
		}
		catch(SQLException e)
	    {
			e.printStackTrace();
	    }
	}
}
